define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dahuis/sbgjj/index',
                    add_url: 'dahuis/sbgjj/add',
                    edit_url: 'dahuis/sbgjj/edit',
                    del_url: 'dahuis/sbgjj/del',
                    multi_url: 'dahuis/sbgjj/multi',
                    table: 'dahuis_sbgjj',
                }
            });

            var table = $("#table");

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["100%", "100%"]);
            $(".btn-audit").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
                $(".btn-auditone").data("area", ["100%", "100%"]);
            });

            table.on('load-success.bs.table', function (e, data) {
                $("#money").text(data.extend.money);
            });

            var buttons = [];
            buttons.push({
                name: 'op1',
                title: __('一键续费'),
                classname: 'btn btn-xs btn-info btn-dialog',
                icon: 'fa fa-cart-plus',
                url: 'dahuis/sbgjj/yjxf'
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                search: false,
                sortName: 'id',
                searchFormVisible: true,
                dblClickToEdit: false, //是否启用双击编辑
                showExport: false,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id'), operate:false, sortable: true},
                        {
                            field: 'operate',
                            width: "90px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: buttons,
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'gsname', title: __('Gsname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'contact', title: __('Contact'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'contacttel', title: __('Contacttel'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'htstart', title: __('Htstart'), addclass:'datetimepicker'},
                        {field: 'htend', title: __('Htend'), addclass:'datetimepicker'},
                        {field: 'htje', title: __('Htje'), operate:false},
                        {field: 'jnr', title: __('Jnr'), operate:false},
                        {field: 'idc', title: __('Idc'), operate:false},
                        {field: 'jnrtel', title: __('Jnrtel'), operate:false},
                        {field: 'gongzi', title: __('Gongzi'), operate:false},
                        {field: 'sbbase', title: __('Sbbase'), operate:false},
                        //{field: 'sbbl', title: __('Sbbl'), operate:false},
                        {field: 'sbje', title: __('Sbje'), operate:false},
                        {field: 'gjjbase', title: __('Gjjbase'), operate:false},
                        //{field: 'gjjbl', title: __('Gjjbl'), operate:false},
                        {field: 'gjjje', title: __('Gjjje'), operate:false},
                        {field: 'gjjzh', title: __('Gjjzh'), operate:false},
                        {
                            field: 'remarks',
                            title: __('Remarks'),
                            editable: {
                                emptytext: '无',
                                rows:2,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符',
                        },
                        {field: 'isxy', title: __('Isxy'), searchList: {"未续约":__('未续约'),"已续约":__('已续约')}, formatter: Table.api.formatter.normal},
                        //{field: 'sbzt', title: __('Sbzt'), searchList: {"未办理":__('未办理'),"已办理":__('已办理')}, formatter: Controller.api.formatter.sbstatus},
                        //{field: 'zczt', title: __('Zczt'), searchList: {"未办理":__('未办理'),"已办理":__('已办理')}, formatter: Controller.api.formatter.zcstatus},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            var form = $("form.form-commonsearch");
            if ($(".datetimepicker", form).size() > 0) {
                require(['bootstrap-datetimepicker'], function () {
                    var options = {
                        format: 'YYYY-MM',
                        icons: {
                            time: 'fa fa-clock-o',
                            date: 'fa fa-calendar',
                            up: 'fa fa-chevron-up',
                            down: 'fa fa-chevron-down',
                            previous: 'fa fa-chevron-left',
                            next: 'fa fa-chevron-right',
                            today: 'fa fa-history',
                            clear: 'fa fa-trash',
                            close: 'fa fa-remove'
                        },
                        showTodayButton: true,
                        showClose: true
                    };
                    $('.datetimepicker', form).parent().css('position', 'relative');
                    $('.datetimepicker', form).datetimepicker(options);
                });
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            document.getElementById('c-gsname').addEventListener('paste', function (e) {
                if ( !(e.clipboardData && e.clipboardData.items) ) {
                    return;
                }
                for (var i = 0, len = e.clipboardData.items.length; i < len; i++) {
                    var item = e.clipboardData.items[i];

                    if (item.kind === "string") {
                        item.getAsString(function (str) {
                            var gsname = str.trim();
                            $("#c-gsname").val(gsname);
                            updategsinfo();
                        })
                    } else if (item.kind === "file") {

                    }
                    break;
                }
            });

            $(document).on("change", "#c-gsname", function () {
                updategsinfo();
            });

            $("#c-gsname").keypress(function (e) {
                if (e.which == 13) {
                    updategsinfo();
                }
            });

            function updategsinfo() {
                var gsname = $("#c-gsname").val();
                gsname = gsname.trim();
                console.log("gsname:" + gsname);
                $.ajax({
                    url: 'dahuinew/dljzyw/getdsinfo',
                    type: 'post',
                    data: {
                        gsname:gsname,
                        isdljzyw:0
                    },
                    dataType: 'json',
                    success: function (ret) {
                        $("#c-htstart").val(ret.data.htstart);
                        $("#c-htend").val(ret.data.htend);
                        $("#c-contact").val(ret.data.contact);
                        $("#c-contacttel").val(ret.data.contacttel);
                        if (ret.data.benyue == 1) {
                            //Backend.api.toastr.error(gsname+"本月已有录入的的订单，请确认是否重复录入");
                        }
                    }
                });
            }

            //社保金额=社保基数＊社保基数
            //公积金金额=公积金基数＊公积金基数
            $(document).on("change", "#c-sbbase", function () {
                updategsinfo1();
            });

            $("#c-sbbase").keypress(function (e) {
                if (e.which == 13) {
                    updategsinfo1();
                }
            });

            function updategsinfo1() {
                var sbbase = parseFloat($("#c-sbbase").val());
                var sbbl = parseFloat($("#c-sbbl").val());
                $("#c-sbje").val(sbbase * sbbl);
            }

            $(document).on("change", "#c-gjjbase", function () {
                updategsinfo2();
            });

            $("#c-gjjbase").keypress(function (e) {
                if (e.which == 13) {
                    updategsinfo2();
                }
            });

            function updategsinfo2() {
                var gjjbase = parseFloat($("#c-gjjbase").val());
                var gjjbl = parseFloat($("#c-gjjbl").val());
                $("#c-gjjje").val(gjjbase * gjjbl);
            }

            //身份证
            document.getElementById('c-idc').addEventListener('paste', function (e) {
                if ( !(e.clipboardData && e.clipboardData.items) ) {
                    return;
                }
                for (var i = 0, len = e.clipboardData.items.length; i < len; i++) {
                    var item = e.clipboardData.items[i];

                    if (item.kind === "string") {
                        item.getAsString(function (str) {
                            var idcname = str.trim();
                            $("#c-idc").val(idcname);
                            updateidcname();
                        })
                    } else if (item.kind === "file") {

                    }
                    break;
                }
            });

            $(document).on("change", "#c-idc", function () {
                updateidcname();
            });

            $("#c-idc").keypress(function (e) {
                if (e.which == 13) {
                    updateidcname();
                }
            });

            function updateidcname() {
                var idcname = $("#c-idc").val();
                idcname = idcname.trim();
                ////http://dahui.test/admin/dahuis/sbgjj/havesamejnr?idc=341221199803066322
                $.ajax({
                    url: 'dahuis/sbgjj/havesamejnr',
                    type: 'post',
                    data: {
                        idc:idcname
                    },
                    dataType: 'json',
                    success: function (ret) {
                        console.log("111", ret);
                        if (ret.code == 0) {
                            $("#c-idc").val("");
                            Backend.api.toastr.error(ret.msg);
                        }
                    }, error: function (e) {
                        $("#c-idc").val("");
                        console.log("222",e);
                    }
                });
            }

            Controller.api.bindevent();
        },
        edit: function () {
            document.getElementById('c-gsname').addEventListener('paste', function (e) {
                if ( !(e.clipboardData && e.clipboardData.items) ) {
                    return;
                }
                for (var i = 0, len = e.clipboardData.items.length; i < len; i++) {
                    var item = e.clipboardData.items[i];

                    if (item.kind === "string") {
                        item.getAsString(function (str) {
                            var gsname = str.trim();
                            $("#c-gsname").val(gsname);
                            updategsinfo();
                        })
                    } else if (item.kind === "file") {

                    }
                    break;
                }
            });

            $(document).on("change", "#c-gsname", function () {
                updategsinfo();
            });

            $("#c-gsname").keypress(function (e) {
                if (e.which == 13) {
                    updategsinfo();
                }
            });

            function updategsinfo() {
                var gsname = $("#c-gsname").val();
                gsname = gsname.trim();
                console.log("gsname:" + gsname);
                $.ajax({
                    url: 'dahuinew/dljzyw/getdsinfo',
                    type: 'post',
                    data: {
                        gsname:gsname
                    },
                    dataType: 'json',
                    success: function (ret) {
                        $("#c-htstart").val(ret.data.htstart);
                        $("#c-htend").val(ret.data.htend);
                        $("#c-contact").val(ret.data.contact);
                        $("#c-contacttel").val(ret.data.contacttel);
                    }
                });
            }

            //社保金额=社保基数＊社保基数
            //公积金金额=公积金基数＊公积金基数
            $(document).on("change", "#c-sbbase", function () {
                updategsinfo1();
            });

            $("#c-sbbase").keypress(function (e) {
                if (e.which == 13) {
                    updategsinfo1();
                }
            });

            function updategsinfo1() {
                var sbbase = parseFloat($("#c-sbbase").val());
                var sbbl = parseFloat($("#c-sbbl").val());
                $("#c-sbje").val(sbbase * sbbl);
            }

            $(document).on("change", "#c-gjjbase", function () {
                updategsinfo2();
            });

            $("#c-gjjbase").keypress(function (e) {
                if (e.which == 13) {
                    updategsinfo2();
                }
            });

            function updategsinfo2() {
                var gjjbase = parseFloat($("#c-gjjbase").val());
                var gjjbl = parseFloat($("#c-gjjbl").val());
                $("#c-gjjje").val(gjjbase * gjjbl);
            }

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            defaultdate: function () {
                var date = new Date();
                var year = date.getFullYear();
                var month = date.getMonth() + 1;//获取当前月份(0-11,0代表1月)
                if (month >= 10) {
                    month = "" + month;
                } else {
                    month = "0" + month;
                }
                //console.log("QQQ---" + year + "-" + month);
                return year + "-" + month;
            },
            validateIdCard: function (idCard) {
                idCard = idCard.trim();
                var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;

                //如果通过该验证，说明身份证格式正确，但准确性还需计算
                if (regIdCard.test(idCard)) {
                    if (idCard.length == 18) {
                        var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
                        var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
                        var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
                        for (var i = 0; i < 17; i++) {
                            idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
                        }
                        var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
                        var idCardLast = idCard.substring(17);//得到最后一位身份证号码
                        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
                        if (idCardMod == 2) {
                            if (idCardLast == "X" || idCardLast == "x") {
                                //alert("恭喜通过验证啦！");
                                return true;
                            } else {
                                //alert("身份证号码错误！");
                                Backend.api.toastr.error("身份证号码验证错误,请修改！");
                                return false;
                            }
                        } else {
                            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                            if (idCardLast == idCardY[idCardMod]) {
                                //alert("恭喜通过验证啦！");
                                return true;
                            } else {
                                //alert("身份证号码错误！");
                                Backend.api.toastr.error("身份证号码验证错误,请修改！");
                                return false;
                            }
                        }
                    }
                } else {
                    //alert("身份证格式不正确!");
                    Backend.api.toastr.error("身份证格式不正确!");
                    return false;
                }
            },
            showMonthFirstDay: function () {
                var Nowdate = new Date();
                var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1);
                M = Number(MonthFirstDay.getMonth()) + 1
                return MonthFirstDay.getFullYear() + "-" + M + "-" + MonthFirstDay.getDate() + ' 00:00:00';
            },
            showMonthLastDay: function () {
                var Nowdate = new Date();
                var MonthNextFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth() + 1, 1);
                var MonthLastDay = new Date(MonthNextFirstDay - 86400000);
                M = Number(MonthLastDay.getMonth()) + 1
                return MonthLastDay.getFullYear() + "-" + M + "-" + MonthLastDay.getDate() + ' 23:59:59';
            },
            defaultdate2: function () {
                //2019-07-01 00:00:00 - 2019-07-31 23:59:59
                var MonthFirstDay = Controller.api.showMonthFirstDay();
                var MonthLastDay = Controller.api.showMonthLastDay();

                return MonthFirstDay + " - " + MonthLastDay;
            },
            formatter: {//渲染的方法
                zcstatus: function (value, row, index) {
                    if (row.zczt != '已办理') {
                        return [
                            '<a href="javascript:;" class="zcstatus text-danger" data-id= "' + row.id + '" > ' + row.zczt + '</a>'
                        ].join("")

                    }  else {
                        return [
                            '<a href="javascript:;" class="zcstatus" data-id= "' + row.id + '" > ' + row.zczt + '</a>'
                        ].join("")
                    }
                },
                sbstatus: function (value, row, index) {
                    if (row.sbzt != '已办理') {
                        return [
                            '<a href="javascript:;" class="sbstatus text-danger" data-id= "' + row.id + '" > ' + row.sbzt + '</a>'
                        ].join("")

                    }  else {
                        return [
                            '<a href="javascript:;" class="sbstatus" data-id= "' + row.id + '" > ' + row.sbzt + '</a>'
                        ].join("")
                    }
                },
            }
        }
    };
    return Controller;
});